#include "../server.hpp"
#include <iostream>
#include <chrono>
#include <thread>
#include <cassert>

int main()
{
    Socket cli_sock;
    cli_sock.CreateClient(8085, "127.0.0.1");
    std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    while(1) {
        assert(cli_sock.Send(req.c_str(), req.size()) != -1);
        char buf[1024] = {0};
        assert(cli_sock.Recv(buf, 1023));
        DBG_LOG("[%s]", buf);
        sleep(3);
    }
    cli_sock.Close();
    return 0;
}

// void logMessage(const std::string& message) {
//     std::cout << message << std::endl;
// }

// int main() {
//     try {
//         Socket cli_sock;
//         if (!cli_sock.CreateClient(8085, "127.0.0.1")) {
//             throw std::runtime_error("Failed to create client socket");
//         }

//         // 构造多个请求字符串
//         std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
//         req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
//         req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";

//         while (true) {
//             if (cli_sock.Send(req.c_str(), req.size()) == -1) {
//                 throw std::runtime_error("Failed to send request");
//             }

//             for (int i = 0; i < 3; ++i) { // 处理每个请求的响应
//                 char buf[1024] = {0};
//                 if (!cli_sock.Recv(buf, 1023)) {
//                     throw std::runtime_error("Failed to receive response");
//                 }
//                 logMessage("Received: " + std::string(buf));
//             }

//             std::this_thread::sleep_for(std::chrono::seconds(3)); // 等待3秒
//         }

//         cli_sock.Close();
//     } catch (const std::exception& e) {
//         logMessage("Error: " + std::string(e.what()));
//         return 1;
//     }

//     return 0;
// }
